import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import { viteMockServe } from "vite-plugin-mock";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
const pathSrc = path.resolve(__dirname, "./src");
export default defineConfig({
  plugins: [
    vue(),
    viteMockServe({
      mockPath: "./mock/", // mack文件地址
      localEnabled: true, // 是否开启开发环境
      proEnabled: true, //生产打包开关，控制关闭mock的时候不让mock打包到最终代码
      logger: true, //是否在控制台显示请求日志
    }),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(pathSrc, "assets/icons")],
      // 指定symbolId格式
      symbolId: "icon-[name]",
    }),
  ],
  resolve: {
    alias: {
      "@": path.resolve("./src"), // 相对路径别名配置
    },
  },
  // 代理配置
  server: {
    // host: "0.0.0.0",
    port: 8080, //本地运行端口
    open: true, // 配置自动启动浏览器
    proxy: {
      // 指定代理所有/api请求
      "/api": {
        // 代理请求之后的请求地址
        target: "http://121.37.5.157:8083/api",
        ws: true,
        // 跨域
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/api/, ""),
        bypass(req, res, options) {
          const proxyUrl =
            new URL(options.rewrite(req.url) || "", options.target as string)
              ?.href || "";
          console.log(proxyUrl);
          req.headers["x-req-proxyUrl"] = proxyUrl; //设置无效
          res.setHeader("x-req-proxyUrl", proxyUrl); //有效
        },
      },
    },
  },
});
